package risk.control.application.component.channel;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import risk.control.application.R;
import risk.control.application.entity.ChannelTypeBean;
import risk.control.application.entity.CurrencyTypeBean;


//币种选择弹出框
public class ChannelDialog extends Dialog implements DialogInterface.OnDismissListener {

    private static final int style = R.style.Dialog;
    private static final boolean isCancelable = false;

    private final View mRootView;
    private Button mBtnConfirm;
    private TextView mTvTitle;
    private ChannelClickListener mConfirmClickListener;

    private ListView mLvDate;
    private List<ChannelTypeBean> mList;
    private ChannelAdapter mAapter;

    public ChannelDialog(Context context, String curryStr) {
        super(context, style);
        mRootView = View.inflate(context, R.layout.channel_type_dialog, null);
        setContentView(mRootView);
        setCancelable(isCancelable);
        setCanceledOnTouchOutside(isCancelable);
        Window window = getWindow();
        if (window != null) {
            // 设置对话框的位置：居中
            WindowManager.LayoutParams params = window.getAttributes();
            //获取屏幕的宽高
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            DisplayMetrics displayMetrics = new DisplayMetrics();
            display.getMetrics(displayMetrics);
            int width = displayMetrics.widthPixels;
            int height = displayMetrics.heightPixels;
            //根据手机屏幕像素动态宽高
            params.width = (int) (width * 0.70);
            params.height = (int) (height * 0.5);
            params.gravity = Gravity.CENTER;
            window.setAttributes(params);
            initView();
            initData(context, curryStr);
            initListener(this);
        }
    }


    private void initView() {
        mTvTitle = mRootView.findViewById(R.id.tv_title);
        mBtnConfirm = mRootView.findViewById(R.id.btn_confirm);
        mLvDate = mRootView.findViewById(R.id.lv_data);
    }

    private void initData(Context context, String curryStr) {
        ChannelTypeBean bean = new ChannelTypeBean();
        bean.channelType = "柜面";
        ChannelTypeBean bean1 = new ChannelTypeBean();
        bean1.channelType = "ATM";
        ChannelTypeBean bean2 = new ChannelTypeBean();
        bean2.channelType = "超柜";
        ChannelTypeBean bean3 = new ChannelTypeBean();
        bean3.channelType = "其他";
        if (mList == null) {
            mList = new ArrayList<>();
        } else {
            mList.clear();
        }
        mList.add(bean);
        mList.add(bean1);
        mList.add(bean2);
        mList.add(bean3);
        //选择当前的币种
        int size = mList.size();
        for (int i = 0; i < size; i++) {
            ChannelTypeBean item = mList.get(i);
            String currencyType = item.channelType;
            if (currencyType.equals(curryStr)) {
                item.isSelect = "0";
            } else {
                item.isSelect = "1";
            }
        }
        //设置适配器
        mAapter = new ChannelAdapter(context, mList);
        mLvDate.setAdapter(mAapter);
    }

    private void initListener(ChannelDialog displayInfoDialog) {
        mLvDate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int size = mList.size();
                for (int i = 0; i < size; i++) {
                    ChannelTypeBean bean = mList.get(i);
                    if (i == position) {
                        bean.isSelect = "0";
                    } else {
                        bean.isSelect = "1";
                    }
                }
                mAapter.refreshData(mList);//刷新页面
            }
        });

        mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mConfirmClickListener != null) {
                    int size = mList.size();
                    for (int i = 0; i < size; i++) {
                        ChannelTypeBean bean = mList.get(i);
                        String isSelect = bean.isSelect;
                        String currencyType = bean.channelType;
                        if (isSelect.equals("0")) {
                            mConfirmClickListener.onPositiveClick(currencyType);
                        }
                    }
                }
                displayInfoDialog.dismiss();
            }
        });
    }


    @Override
    public void onDismiss(DialogInterface dialog) {

    }

    public void setBtnClickListener(ChannelClickListener listener) {
        this.mConfirmClickListener = listener;
    }

    public void setCancelable(boolean isCancelable) {
        setCanceledOnTouchOutside(isCancelable);
    }


    public void showDialog() {
        this.show();
    }


}
